<?php
//LAST MODIFIED AT - 21/11/03 01:45AM	BY 	RIzwAN

//session_start();


?>
<?

if ($_POST['Delete'] == 1) {
	
	$sql = "SELECT visit_id FROM er_visit WHERE client_id = ".$_POST['txtClientID'];
	$result = r_db($sql);
	
	while ($row = mysql_fetch_array($result)) {
		$visit_id = $row['visit_id'];
		//echo $visit_id."<br />";
		
		$sql = "DELETE FROM er_visit WHERE visit_id = $visit_id";
		r_db($sql);
		//echo $sql."<br />";
		
		$sql = "DELETE FROM er_relief_on_visit WHERE visit_id = $visit_id";
		r_db($sql);
		//echo $sql."<br />";
		
		$sql = "DELETE FROM er_payment_on_visit WHERE visit_id = $visit_id";
		r_db($sql);
		//echo $sql."<br />";
		
		$sql = "DELETE FROM er_client_income WHERE visit_id = $visit_id";
		r_db($sql);
		//echo $sql."<br />";
	}
	
	$sql = "DELETE FROM er_client WHERE client_id = ".$_POST['txtClientID'];
	r_db($sql);
	//echo $sql."<br />";
	
	echo "Client successfully deleted.";
	echo "<br /><a href=\"?page=search_basic\">Click here to return to the search screen.</a>";
}
else {
	//start not deleting loop\
	
	$originalClientId =$HTTP_POST_VARS['txtClientID'];
	
	
	
	$cmbIdType = $HTTP_POST_VARS['cmbIdType'];
	
	$txtNumber  = $HTTP_POST_VARS['txtNumber'];
	
	$idnumhyphen=explode('-',$txtNumber);
		$txtNumber = "";
		for ($i=0;$i<count($idnumhyphen);$i++) 
		{
				$txtNumber=$txtNumber."". $idnumhyphen[$i];
		}
		
		$idnumspace=explode(' ',$txtNumber);
		$txtNumber = "";
		for ($i=0;$i<count($idnumspace);$i++) 
		{
			$txtNumber=$txtNumber."". $idnumspace[$i];
		}
		
		$idnumdot=explode('.',$txtNumber);
		$txtNumber = "";
		for ($i=0;$i<count($idnumdot);$i++) 
		{
			$txtNumber=$txtNumber."". $idnumdot[$i];
		}
		
		$idnumslash=explode('/',$txtNumber);
		$txtNumber = "";
		for ($i=0;$i<count($idnumslash);$i++) 
		{
			$txtNumber=$txtNumber."". $idnumslash[$i];
		}
		
		$idnumbslash=explode('\\',$txtNumber);
		$txtNumber = "";
		for ($i=0;$i<count($idnumbslash);$i++) 
		{
			$txtNumber=$txtNumber."". $idnumbslash[$i];
		}
	
	
	$optGender  = $HTTP_POST_VARS['optGender'];
	
	$txtGivenName = $HTTP_POST_VARS['txtGivenName'];
	
	$txtNationality = $HTTP_POST_VARS['txtNationality'];
	
	$txtPreferredLanguage  = $HTTP_POST_VARS['txtPreferredLanguage'];
	
	$txtSurname = $HTTP_POST_VARS['txtSurname'];
	
	
	
	$txtOtherIdType = $HTTP_POST_VARS['txtOtherIdType'];
	
	$txtDob = $HTTP_POST_VARS['txtDob'];
	
	
	
	$queryPayment = "SELECT * FROM er_Payment";
	
	$resultPayment = r_db($queryPayment);
	
	
	
	$i=0;
	
	if($optGender=="Male")
	
	{
	
		$gender = "M";
	
	}
	
	elseif($optGender=="Female")
	
	{
	
		$gender = "F";
	
	}
	
	
	
	$DOB =  $txtDob;
	
	
	
	
	
	$DbDOB=explode("/",$txtDob);
	
	$dayDOB=$DbDOB[0];
	
	@$monthDOB=$DbDOB[1];
	
	@$yearDOB=$DbDOB[2];
	
	$txtDob=$yearDOB."-".$monthDOB."-".$dayDOB;
	
	
	
	$current_date = date("Y-m-d");
	
	$identificationNo = getIdFromListBox("er_Identification_Type","identification_type", $cmbIdType);
	
	
	
	
	
	$queryClientTable = "
	
							UPDATE er_Client SET
	
							id_reference_no='$txtNumber',
	
							surname='$txtSurname',
	
							givenname='$txtGivenName',
	
							dob='$txtDob',
	
							nationality='$txtNationality',
	
							gender='$gender',
	
							preferred_language='$txtPreferredLanguage',
	
							other_id_type_specified='$txtOtherIdType',
	
							identification_no=$identificationNo
	
							WHERE client_id='$originalClientId'";		
	
	$results = r_db($queryClientTable);
	
	//echo "lient ID is : $originalClientId";
	
	echo "<b><span style=\"font-weight: normal\">Record of <b>$txtGivenName $txtSurname</b> has been successfully updated!</b>";
	//echo"<table width=\"100%\" border=\"0\"><tr><td align=\"right\" ><a href=\"#\" onClick=\"history.go(-2)\">Client Main Page</a></td></tr></table>";
	//echo"<table width=\"100%\" border=\"0\"><tr><td align=\"right\" ><a href=\"viewClientTemp.php?clientID=$originalClientId\">Client Main Page</a></td></tr></table>";
	
	
	
	//============================ CODE TO DISPLAY THE UPDATED FIELDS =======================
	print "<p>";
	
	if (isset($_SESSION['session_clientId']))
		$clientID = $_SESSION['session_clientId'];	
	else 
		echo "Client ID not set.";
	
	$myArray = array();
	
	if ($HTTP_POST_VARS)
	$myArray = $HTTP_POST_VARS;
	
	//print "<br>client is:" . $clientID . "<br>";
	if (isset($clientID))
	{
		$query = "SELECT * FROM er_Client WHERE client_id ='$clientID'";
		$sql = r_db($query);
		$row = mysql_fetch_array($sql);
		$idtyperesult=getIDtype($row[10]);
		echo "<table border=\"0\" width=\"100%\"> ";
		echo "<tr><th>Client Details</th></tr>";
		echo  "<tr><td width=\"17%\"><b>Surname:</b><td width=\"32%\">$row[2]</td><td width=\"17%\"><b>Given Name:</b><td width=\"32%\">".$row[3]."</td></tr>";
	
		$DOB =  $row[4];
		$DbDOB=explode("-",$DOB);
		$dayDOB=$DbDOB[2];
		$monthDOB=$DbDOB[1];
		$yearDOB=$DbDOB[0];
		$DOB=$dayDOB."/".$monthDOB."/".$yearDOB;
	
		echo  "<tr>"
		.	"<td><b>Date of Birth:</b><td>" . $DOB;
		echo "<td><b>Nationality:</b></span><td>" . $row[5]. " </td>";
		echo  "</tr>";
		
		echo"<tr>";
		echo "<td><b>Gender:</b><td>$optGender</td>";
		echo "<td><b>Preferred Language:</b><td>" . $row[7]. " </td>";	
		echo "</tr>";

		echo "<tr>"
		.	"<td><b>ID Type:</b><td>".$idtyperesult[0]. "</td>"
		.     "<td><b>ID Reference No:</b></td><td>" . $row[1]."</td>";
		echo "</tr>";
		$otheridvalue = $row[8];
		if ($otheridvalue != null)
		{
			echo   "<tr><td>&nbsp;</td><td>&nbsp;</td><td><b>Other ID Type:</b></td><td>" . $row[8]."</td></tr>";
		}


		print "   <input type=\"hidden\" name=\"selectedClientID\" value=\"". $row[0] . "\">"; //</td>";
		print "   <input type=\"hidden\" name=\"selectedClientSurname\" value=\"". $row[2] . "\">";
		print "   <input type=\"hidden\" name=\"selectedClientFirstName\" value=\"". $row[3] . "\">";
		echo "</table>";
		echo "</body>";
		echo "</link></html>";
		print "<input type=\"hidden\" name=\"selectedClientID\" value=\"". $row[0] . "\">"; //</td>";
	
		echo
	
		"</table>";
	}
	echo "</p>";
}
//end not deleting loop

//Function start here++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++					
function getIDtype($idtype)
{
	$query="select identification_type from er_Identification_Type where identification_no='".$idtype."'";
	$resultIDtype = r_db($query);
	$row=mysql_fetch_array($resultIDtype);
	return $row;
}
?>

